package com.hadoop.fof;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class FOFJob {
    public static void main(String[] args) {
        Configuration conf = new Configuration(true);
        //在 Windows 环境中 run mapreduce 需要此条配置
        //除了 GUN/Linux 运行客户端都需要设置为 true
        conf.set("mapreduce.app-submission.cross-platform","true");
        //本地模拟需要此条配置，配置文件(mapred-site.xml)的值为 yarn
        conf.set("mapreduce.framework.name", "local");
        try {
            Job job = Job.getInstance(conf);
            //jar 包是集群模式运行不可缺少的
            job.setJarByClass(FOFJob.class);

            //本地多线程模拟不需要打 jar 包
            //job.setJar("E:\\package\\FoF.jar")

            job.setMapperClass(FMapper.class);
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);

            job.setReducerClass(FReducer.class);

            Path input = new Path("/user/friends/input");
            FileInputFormat.addInputPath(job,input);
            Path output = new Path("/user/friends/output");
            if(output.getFileSystem(conf).exists(output)){
                output.getFileSystem(conf).delete(output);
            }
            FileOutputFormat.setOutputPath(job,output);

            job.waitForCompletion(true);


        } catch (IOException | InterruptedException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
